<template>
    <div>
        <div v-if="show">
            <!--  本科生培养计划审核  -->
            <template v-if="isUndergraduatePlan">
                <ongoing-detail
                    :detailRow="dataInfo"
                    ref="ongoingDetail"
                    v-if="ongoingDetailShow"
                />
                <end-detail :detailRow="dataInfo" ref="endDetail" v-else />
            </template>
            <!--  研究生培养计划审核  -->
            <postgraduate-approve-detail :publicInfo="dataInfo" v-if="isPostgraduatePlan" />
            <!--本科生培养方案审核 -->
            <approve-detail
                :dataInfo="dataInfo"
                :isFromPulic="isFromPulic"
                ref="approveDetail"
                v-if="isCulture"
            />
            <ongoing-course-detail
                :detailRow="dataInfo"
                ref="ongoingCourseDetail"
                v-if="isCourseApprove"
            />
            <minor-culture-detail
                :publicInfo="dataInfo"
                ref="minorCultureDetail"
                v-if="isMinorCultureApprove"
            />
            <!--交流申请 -->
            <exchange-approve-detail
                :publicInfo="dataInfo"
                ref="exchangeApproveDetail"
                v-if="isExchangeApprove"
            />
            <credit-detail :publicInfo="dataInfo" ref="creditDetail" v-if="iscreditDetail" />
            <!-- 免修免听审核 -->
            <exempt-audit-detail
                :publicInfo="dataInfo"
                ref="exemptAuditDetail"
                v-if="isExemptAuditDetail"
            />
            <progress-detail
                :publicInfo="dataInfo"
                ref="exemptAuditDetail"
                v-if="isProgressDetail"
            />
            <!-- 论文选题 --->
            <approval-topic
                ref="approvalTopic"
                :detailData="detailData"
                :normalTopicEdit="normalTopicEdit"
                @goBack="showRollDetail"
                v-if="isApprovalTopic"
            />
            <normal-topic-cat
                ref="normalTopicCat"
                :detailData="detailData"
                @preview="catPreview"
                @goBack="showRollDetail"
                v-if="isNormalTopicCat && !currentKey"
            />
            <preview
                v-if="!isNormalTopicCat && currentKey == 'preview-normalTopic'"
                :paperName="previewDetail.paperName"
                :status="previewDetail.status"
                :preFlag="true"
                :paperId="detailData.paperId"
                :chapterList="previewDetail.chapterList"
                @pre="isNormalTopicCat = true"
                @goBack="showRollDetail"
            />
            <!-- 中期考核 -->
            <mid-approval-topic
                ref="midApprovalTopic"
                :detailData="detailData"
                :normalTopicEdit="normalTopicEdit"
                @goBack="showRollDetail"
                v-if="isMidApprovalTopic"
            />
            <mid-approval-cat
                ref="catPlan"
                :studentId="detailData.studentId"
                :applyId="detailData.applyId"
                :detailData="detailData"
                @preview="catPreview"
                @goBack="showRollDetail"
                v-if="isMidApprovalCat && !currentKey"
            />
            <mid-preview
                v-if="!isMidApprovalCat && currentKey == 'preview-midTopic'"
                :paperName="previewDetail.paperName"
                :status="previewDetail.status"
                :preFlag="true"
                :applyId="detailData.applyId"
                :chapterList="previewDetail.chapterList"
                @pre="isMidApprovalCat = true"
                @goBack="showRollDetail"
            />
            <room-borrow
                v-if="isRoomBorrow"
                :dataInfo="detailData"
            />
        </div>
    </div>
</template>

<script>
import { queryAllPaperInfo } from 'common/src/api/thesisSelection/workApply'
import { queryAllAssessInfo } from 'common/src/api/midTermExamination/workApply'
import UTILS from 'common/src/utils/utils'
import { queryDic } from 'common/src/api/dictionary'
import { getApplyInfoByWorkflowNo } from 'common/src/api/resource/classroom'
export default {
    props: ['dataInfo', 'isFromPulic', 'fromCheckDetail'],
    data() {
        return {
            ongoingDetailShow: false,
            isUndergraduatePlan: false, //本科生
            isPostgraduatePlan: false, //研究生
            isCulture: false,
            isPlan: false,
            isCourseApprove: false, // 培养计划课程审批
            isMinorCultureApprove: false, // 辅修培养方案审批
            isExchangeApprove: false, //交流审请
            iscreditDetail: false, //学分认定详情
            isExemptAuditDetail: false, //免修免听审核
            isProgressDetail: false, //教学进度
            normalTopicEdit: this.fromCheckDetail,
            isApprovalTopic: false, //必修正常选题
            isNormalTopicCat: false,
            isMidApprovalTopic: false,
            isMidApprovalCat: false,
            detailData: {},
            previewDetail: {},
            currentKey: '',
            options: {
                reasonType: [],
            },
            isRoomBorrow: false, // 教室借用
        }
    },
    components: {
        approveDetail: (resolve) => require(['common/src/views/culture/bachelor/approvalDetail'], resolve),
        endDetail: (resolve) => require(['common/src/views/ugdCulturePlan/planApproval/endDetail'], resolve),
        ongoingDetail: (resolve) =>
            require(['common/src/views/ugdCulturePlan/planApproval/ongoingDetail'], resolve),
        postgraduateApproveDetail: (resolve) =>
            require(['common/src/views/culture/plan/approveDetail'], resolve),
        ongoingCourseDetail: (resolve) =>
            require(['common/src/views/ugdCulturePlan/planApproval/ongoingCourseDetail'], resolve),
        minorCultureDetail: (resolve) =>
            require(['common/src/views/minor/minorCultureScheme/CultureSchemeRollDetail'], resolve),
        exchangeApproveDetail: (resolve) =>
            require(['common/src/views/internationalExchanges/underGraduate/approvalDetail'], resolve),
        creditDetail: () => import('common/src/views/internationalExchanges/underGraduate/creditDetail'),
        exemptAuditDetail: () => import('common/src/views/exempt/auditDetail'),
        progressDetail: () => import('common/src/views/progress/progressDetail'),
        approvalTopic: () => import('common/src/views/thesisSelection/component/ApprovalTopic'),
        normalTopicCat: () => import('common/src/views/thesisSelection/component/CatPlan'),
        preview: () => import('common/src/views/thesisSelection/component/Preview'),
        midApprovalTopic: () => import('common/src/views/midTermExamination/component/ApprovalTopic'),
        midApprovalCat: () => import('common/src/views/midTermExamination/component/CatPlan'),
        midPreview: () => import('common/src/views/midTermExamination/component/Preview'),
        roomBorrow: () => import('common/src/views/resource/classroom/roomBorrowingInfo'),
    },
    created() {
        this.init()
    },
    methods: {
        init() {
            let { wfType, content } = this.dataInfo || {};
            this.initVariables(wfType, content);
            this.isUndergraduatePlan = this.isPlan && content.projID == 1;
            this.isPostgraduatePlan = this.isPlan && content.projId == 2;
        },
        async initVariables(wfType, content) {
            content = content || {};
            let params = {}, list;
            switch (wfType) {
                case 11:
                    this.isCulture = true;
                    break;
                case 18:
                    this.isPlan = true;
                    break;
                case 189:
                    this.isCourseApprove = true;
                    break;
                case 61:
                    this.isMinorCultureApprove = true;
                    break;
                case 154:
                case 152:
                    this.setShowAuditBtn();
                    this.isExchangeApprove = true;
                    break;
                case 155:
                case 153:
                    this.setShowAuditBtn();
                    this.iscreditDetail = true;
                    break;
                case 140:
                    this.setShowAuditBtn();
                    this.isExemptAuditDetail = true;
                    break;
                case 43:
                    this.isProgressDetail = true;
                    break;
                case 66:
                case 65:
                    params = {
                        pageNum_: 1,
                        pageSize_: 20,
                        paperId: content.progressContents && content.progressContents.paperId || '',
                    };
                    ({ list } = await this.getDetailData(queryAllPaperInfo, params));
                    this.detailData = (list && list[0]) || {};
                    this.detailData.approvalId = this.dataInfo.no;
                    if (this.isFromPulic) {
                        this.isNormalTopicCat = true;
                    }else {
                        this.isApprovalTopic = true;
                    }
                    break;
                case 67:
                case 68:
                    params = {
                        pageNum_: 1,
                        pageSize_: 20,
                        applyId: content.progressContents && content.progressContents.applyId || '',
                    };
                    ({ list } = await this.getDetailData(queryAllAssessInfo, params));
                    this.detailData = (list && list[0]) || {};
                    this.detailData.approvalId = this.dataInfo.no;
                    this.detailData.applyCauseTips = '';
                    this.getOptions();
                    if (this.isFromPulic) {
                        this.isMidApprovalCat = true;
                    }else {
                        this.isMidApprovalTopic = true;
                    }
                    break;
                case 64:
                    params = this.dataInfo.no;
                    list = await this.getDetailData(getApplyInfoByWorkflowNo, params);
                    this.detailData = list || {};
                    let towerCode = this.detailData.classroomApplyDetailVoList && this.detailData.classroomApplyDetailVoList[0] && this.detailData.classroomApplyDetailVoList[0].towerCode;
                    this.detailData.towerCode = towerCode && towerCode.split(",");
                    this.detailData.calendarId = this.getCalendarIdByList(this.detailData.classroomApplyDetailVoList);
                    this.detailData.pageType = "3";
                    if (this.isFromPulic) {
                        this.isRoomBorrow = true;
                    }
                    break;
            }
        },
        getCalendarIdByList(list) {
            let str = "";
            if (list != null) {
                str = list[0] && list[0].calendarId || '';
            }
            return str;
         },
        async getDetailData(api, param) {
            let list = {};
            let { code, data } = await api(param);
            if (code == 200) {
                list = data;
            }
            return list;
        },
        setShowAuditBtn() {
            if (this.isFromPulic || this.fromCheckDetail) {
                this.dataInfo.isApproveShow = 1;
            }else {
                this.dataInfo.isApproveShow = 0;
            }
        },
        showRollDetail() {
            if (this.isFromPulic) {
                this.$router.push('/StudentRollCheck');
            }else if (this.fromCheckDetail) {
                this.$router.push('/TeacherRollCheckApproved');
            }else {
                this.$router.push('/TeacherRollCheckUnapproved');
            }
        },
        checkDetail() {
            this.showRollDetail();
        },
        catPreview(chapterList, paperName, status) {
            this.previewDetail = {
                paperName,
                status,
                chapterList,
            };
            if (this.isNormalTopicCat) {
                this.currentKey = 'preview-normalTopic';
                this.isNormalTopicCat = false;
            }
            if (this.isMidApprovalCat) {
                this.currentKey = 'preview-midTopic';
                this.isMidApprovalCat = false;
            }
        },
        getOptions() {
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['R_ApplyReason']
            };
            queryDic(data).then(({ code, data }) => {
                if (code != 200) return;
                this.options.reasonType = [];
                UTILS.fillSelect(
                    {
                        reasonType: 'R_ApplyReason',
                    },
                    data,
                    this.options
                );
                this.detailData.applyCauseTips = this.options.reasonType
                    .filter((el) => {
                        return el.value == this.detailData.applyCause
                    })
                    .map((item) => {
                        return item.label
                    })[0];
            })
        },
    },
    computed: {
        show() {
            let show = Object.keys(this.dataInfo || {}).length;
            if (show) {
                this.ongoingDetailShow = this.dataInfo.result === 'Process';
            }
            if ((this.isFromPulic || this.fromCheckDetail) && this.ongoingDetailShow) {
                this.ongoingDetailShow = false;
                !this.isUndergraduatePlan && (this.dataInfo.result = '');
            }
            return !!show;
        }
    },
}
</script>

<style>
</style>